package com.cyy.maven.core.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.MDC;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyy.maven.core.common.JsonData;
import com.cyy.maven.core.entity.SysUser;
import com.cyy.maven.core.service.SysyUserService;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: Cyy
 * @Description:
 * @Date:Created in 15:46 2018/7/16
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private SysyUserService sysyUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonData login(SysUser user, HttpServletRequest request, ModelMap model) {
        SysUser resultUser = sysyUserService.login(user);
        log.info("request: user/login , user: " + user.toString());
        if (resultUser == null) {
            request.setAttribute("user", user);
            request.setAttribute("errorMsg", "请认真核对账号、密码！");
            return JsonData.fail("请认真核对账号、密码！");
            // return "login";
        } else {
            // System.out.println(resultUser.toString());
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", resultUser);
            MDC.put("Account", user.getAccount());
            // return "redirect:/main.jsp";
            return JsonData.success(resultUser);
        }
    }

}
